<?php

namespace common\models\member;

use Yii;
use common\helpers\Url;
use common\helpers\ColorHelper;

/**
 * This is the model class for table "{{%member_type}}".
 *
 * @property int $id
 * @property string $name 类型名称
 * @property string $icon 图标
 * @property string $thumb 缩略图
 * @property string $name_color 用户名颜色
 * @property string $avatar_frame 头像边框
 * @property string $description 类型介绍
 * @property int $status 启用状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MemberType extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_type}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'string', 'max' => 50],
            [['name_color'], 'string', 'max' => 7],
            [['icon', 'avatar_frame', 'thumb', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '类型名称',
            'icon' => '图标',
            'thumb' => '缩略图',
            'avatar_frame' => '头像边框',
            'name_color' => '用户名颜色',
            'description' => '类型介绍',
            'status' => '启用状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getAvatarFrame() {
        return Url::attach($this->avatar_frame);
    }

    public function getNameColor() {
        return ColorHelper::format($this->name_color);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'icon' => function() {
                return Url::attach($this->icon);
            },
            'cover' => function() {
                return Url::attach($this->thumb);
            },
            'description',
            'link' => function() {
                return Url::weblink('member/apply/{id}', [
                    'id' => $this->id
                ]);
            },
        ];
    }

}
